﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace BlogTruyen
{
    public partial class Reading1 : PhoneApplicationPage
    {

        DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 5) };
        DispatcherTimer timer1 = new DispatcherTimer() { Interval = new TimeSpan(100) };
        ViewModels.Reading Data;
        private bool isMouseTap;

        public Reading1()
        {
            InitializeComponent();

            Data = new ViewModels.Reading();
            DataContext = Data;
            timer.Tick += timer_Tick;
            timer1.Tick += timer1_Tick;
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                var t = webbrowser.InvokeScript("eval", "document.getElementById('ads')");
                if ( t!= null)
                {
                    webbrowser.InvokeScript("eval", @"document.getElementById('ads').style.display = 'none';
document.getElementsByClassName('page-div-select')[0].style.display = 'none';
document.getElementsByTagName('span')[0].style.display = 'none';
document.getElementsByClassName('chonchap')[0].style.display = 'none';
document.getElementsByClassName('page-footer')[0].style.display = 'none';");
                    timer1.Stop();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            controlBar.Visibility = Visibility.Collapsed;
            timer.Stop();
        }
        private void controlBar_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            controlBar.Visibility = Visibility.Collapsed;
        }

        private void RadImageButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if ((this.Orientation & PageOrientation.Landscape) == 0)
            {
                this.SupportedOrientations = SupportedPageOrientation.Landscape;
            }
            else
            {
                this.SupportedOrientations = SupportedPageOrientation.Portrait;
            }
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((this.Orientation & PageOrientation.Landscape) == 0)
            {
                btnRotate.RestStateImageSource = new BitmapImage(new Uri("/Assets/Icons/dark/appbar.transform.rotate.right.png", UriKind.Relative));
            }
            else
            {
                btnRotate.RestStateImageSource = new BitmapImage(new Uri("/Assets/Icons/dark/appbar.transform.rotate.left.png", UriKind.Relative));
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //if (!Data.isLoaded)
            //{
            //    Data.chapter_name = NavigationContext.QueryString["title"];
            //    var webClient = API.GetWebClient();
            //    webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
            //    webClient.DownloadStringAsync(new Uri(string.Format("http://blogtruyen.com{0}", NavigationContext.QueryString["url"]), UriKind.Absolute));
            //    busyIndicator.IsRunning = true;
            //}

            tbChapterName.Text = NavigationContext.QueryString["title"];
            webbrowser.Navigate(new Uri(string.Format("http://m.blogtruyen.com{0}", NavigationContext.QueryString["url"]), UriKind.Absolute));
            timer.Start();
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (API.CheckWebClient(e))
            {
                Data.LoadHtml(e.Result);
                busyIndicator.IsRunning = false;
                webbrowser.NavigateToString(Data.content);
                timer.Start();
            }
        }

        private void webbrowser_Loaded(object sender, RoutedEventArgs e)
        {
            webbrowser.IsScriptEnabled = true;
            timer1.Start();
        }

        private void webbrowser_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isMouseTap = true;
        }

        private void webbrowser_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            isMouseTap = false;
        }

        private void webbrowser_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (isMouseTap)
            {
                if (controlBar.Visibility == Visibility.Collapsed)
                {
                    controlBar.Visibility = Visibility.Visible;
                    timer.Start();
                }
                else
                {
                    controlBar.Visibility = Visibility.Collapsed;
                }
            }
        }
    }
}